<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    class Queue {
      constructor () {
        this.items = []
      }
      push (element) {
        this.items.push(element)
      }
      shift () {
        return this.items.shift()
      }
      isEmpty () {
        return this.items.length === 0
      }
      size () {
        return this.items.length
      }
      front () {
        return this.items[0]
      }
      toString () {
        const result = []
        Object.keys(this.items).forEach(index => {
          result.push(this.items[index], '<-')
        })
        result.pop()
        return result.join(' ')
      }
    }
    // const queue = new Queue()
    // queue.push(1)
    // queue.push(2)
    // queue.push(3)
    // queue.push(4)
    // queue.push(5)
    // queue.shift()
    // console.log(queue.toString())
    function gameQueue (arr, finalNum) {
      const queue = new Queue()
      arr.forEach(value => {
        queue.push(value)
      })
      while (queue.size() > 1) {
        for (let i = 0; i < finalNum; i++) {
          if (i === finalNum - 1) {
            queue.shift()
            // console.log(111)
          } else {
            queue.push(queue.shift())
          }
        }
        // console.log(queue.toString())
      }
      return queue.toString()
    }
    const result = gameQueue(['a', 'b', 'c', 'd'], 3)
    console.log(result)

  </script>
</body>
</html>